package com.chalmers.foa.gui.menus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.XmlResourceParser;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.chalmers.foa.R;
import com.chalmers.foa.core.Game;

public class SelectLevel extends ListActivity{
	/**
	 * Method to create buttons for all existing levels in the game.
	 */
	private List<String> names = new ArrayList<String>();
	private List<Integer> ids= new ArrayList<Integer>();
	private boolean started=false;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setResult(0);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// Forces landscape mode
		int lvl = R.xml.atutorial001;
		int index=0;
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		int currentLevel = pref.getInt("level", R.xml.level001)-R.xml.atutorial001;
		//make buttons for all existing levels
		while(true){
			try{
				this.getResources().getXml(lvl + index);
				String name="";
				XmlResourceParser xrp = getResources().getXml(lvl+index);//_level
				while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
                    if (xrp.getEventType() == XmlResourceParser.START_TAG||
                    		xrp.getEventType() == XmlResourceParser.END_TAG){}
                    else if (xrp.getEventType() == XmlResourceParser.TEXT){
						name=xrp.getText();
					}
                    xrp.next();
				}
				xrp.close();
				ids.add(lvl+index);
				if(ids.size()>currentLevel+1)
					name="Locked";
				names.add(name);
				index++;
			}
			catch(NotFoundException e){
				break;
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ListAdapter adapter = new LevelListItem(this.getApplicationContext(), android.R.layout.simple_list_item_1,currentLevel, names);
		setListAdapter(adapter);
		for(int i=0;i<adapter.getCount();i++){
//			getListView().getChildAt(i)
//			 
//			adapter.getView(i, getListView(), getListView()).setEnabled(false);;
//			getListView().getChildAt(i).setEnabled(false);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l, v, position, id);
		if(!started){
			started=true;
			setResult(ids.get(position));
    		Log.v("222",""+ids.get(position));
			finish();
		}
	}
	private class LevelListItem extends ArrayAdapter<String>{

		int level;
		public LevelListItem(Context context, int resource,int _level,List<String> objects) {
			super(context, resource,objects);
			level=_level;
		}
		
		@Override
		public boolean isEnabled(int position){
			return position<=level;
		}
	}
} 